package com.sczmgk.track.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Xfermode;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;

/**
 * 在图片外面放圆形覆盖图片
 * @author Administrator
 *
 */
public abstract class MaskedImage extends ImageView {
    private static final Xfermode MASK_XFERMODE;
    /**
     * 圆形位图覆盖图片
     */
    private Bitmap mask;
    /**
     * 画笔
     */
    private Paint paint;
    
  /**
   * 1. Xfermode
           可以通过修改Paint的Xfermode来影响在Canvas已有的图像上面绘制新的颜色的方式 。
   */
    static {  
        PorterDuff.Mode localMode = PorterDuff.Mode.DST_IN;
        MASK_XFERMODE = new PorterDuffXfermode(localMode);
    }  
  
    public MaskedImage(Context paramContext) {
        super(paramContext);  
    }  
  
    public MaskedImage(Context paramContext, AttributeSet paramAttributeSet) {
        super(paramContext, paramAttributeSet);  
    }  
  
    public MaskedImage(Context paramContext, AttributeSet paramAttributeSet, int paramInt) {
        super(paramContext, paramAttributeSet, paramInt);  
    }  
  
    /**
     *   创建覆盖图片
     * @return
     */
    public abstract Bitmap createMask();
  
    /**
     * 1.取得ImageView上的图片
     * 2.调用取得图片的  localDrawable.draw(paramCanvas),传入画布
     * 3.在paramCanvas画布上画出覆盖图片
     */
    @Override
	protected void onDraw(Canvas paramCanvas) {
		Drawable localDrawable = getDrawable();//取得ImageView上的设置的图片
		if (localDrawable == null) {
			Log.e("TEST", "localDrawable == null");
			return;
		}

		try {
			if (this.paint == null) {//设置三支画笔的颜色
				Paint localPaint1 = new Paint();
				this.paint = localPaint1;
				this.paint.setFilterBitmap(false);//抗钜齿
				Paint localPaint2 = this.paint;
				Xfermode localXfermode1 = MASK_XFERMODE;
				@SuppressWarnings("unused")
				Xfermode localXfermode2 = localPaint2.setXfermode(localXfermode1);  //可以通过修改Paint的Xfermode来影响在Canvas已有的图像上面绘制新的颜色的方式 。
			}
			//设置画布
			float width_f1 = getWidth();
			float height_f1 = getHeight();
			int saveLayer = paramCanvas.saveLayer(0.0F, 0.0F, width_f1, height_f1, null, 31);//设置画布
			int width = getWidth();
			int height = getHeight();
			localDrawable.setBounds(0, 0, width, height);//设置图片在ImageView上的位置
			localDrawable.draw(paramCanvas);//给图片设置到画布
			if ((this.mask == null) || (this.mask.isRecycled())) {
				Bitmap localBitmap1 = createMask();//取得覆盖图片
				this.mask = localBitmap1;
			}
			//圆形位图覆盖图片
			Bitmap localBitmap2 = this.mask;
			Paint localPaint3 = this.paint;
			paramCanvas.drawBitmap(localBitmap2, 0.0F, 0.0F, localPaint3);//画覆盖图片
			paramCanvas.restoreToCount(saveLayer);
			return;
		} catch (Exception localException) {
			StringBuilder localStringBuilder = new StringBuilder().append("Attempting to draw with recycled bitmap. View ID = ");
		}
	}  
}  